<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
	String callback = request.getParameter("callback");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String purchase_id = request.getParameter("purchase_id") == null ? "" : request.getParameter("purchase_id");
	String product_in_id = request.getParameter("product_in_id") == null ? "" : request.getParameter("product_in_id");
	boolean isAdd = "".equals(product_in_id);
%>
<html>
	<head>
		<title>入库单</title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
		$(function(){
			<%if(isAdd){%>
			$("#operation_date").datetimebox('setValue',getTodaySecond());
			var code="";
			var gd_id="";
			
				$.ajax({
					type:'GET',
					async:false,
					url:'<%=contextPath %>/springMVC/warehouse/getPurchaseById?purchase_id=<%=purchase_id%>',
					success:function(rtJson){
						$('#gd_code').val(rtJson.gd_code);
						$('#gd_name').val(rtJson.gd_name);
						$('#supplier').val(rtJson.company1);
						code=rtJson.code;
						gd_id=rtJson.gd_id;
					}
				});
						$.ajax({
							type:'GET',
							async:false,
							url:'<%=contextPath %>/springMVC/warehouse/getPurchaseProductInCount?purchase_id=<%=purchase_id%>',
							success:function(rtJson){
								if(rtJson.num>0){
									$('#code').val(code+"-"+(parseInt(rtJson.num)+parseInt(1)));	
								}else{
									$('#code').val(code+"-"+1);	
								}
							}
					});
			<%}else{%>
				$('#ff').form('load','<%=contextPath %>/springMVC/warehouse/getPurchaseProductInById?product_in_id=<%=product_in_id%>');
			<%}%>
			$('#table').datagrid({
				<%=isAdd?"url:'"+contextPath+"/springMVC/warehouse/listPurchaseDetail?purchase_id="+purchase_id+"',":"url:'"+contextPath+"/springMVC/warehouse/listPurchaseProductInDetail?product_in_id="+product_in_id+"',"%>
				rownumbers:true,
				fitColumns:true,
				frozenColumns:[[
					{field:'ck',checkbox:true},
				    {title:'材料id',field:'material_id',hidden:true}
				]],
				columns:[[
					{title:'产品编号',field:'code',width:150,align:'center'},
					{title:'产品名称',field:'name',width:300,align:'center'},
					{title:'规格/型号',field:'format_type',width:200,align:'center'},
					{title:'单位',field:'unit_name',width:100,align:'center'},
					<%if(!"1".equals(isShow)){%>
					{title:'申请数量',field:'num',width:100,align:'center'},
					{title:'已到数量',field:'receive_num',width:100,align:'center'},
					<%}%>
					{title:'到货数量',field:'inNum',width:100,align:'center',editor:{
						type:'numberbox',
						options:{
							min:0,
							precision:2
						}
					},formatter:function(value,row,index){
						if(value == undefined){
							 row.inNum = parseFloat(row.num)-parseFloat(row.receive_num);
							 return row.inNum;
						}else{
							return value;
						}
					}}
				]],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				/**
				onAfterEdit:function(rowIndex, rowData, changes){
					if((parseFloat(rowData.inNum)-(parseFloat(rowData.num)-parseFloat(rowData.receive_num)))>0){
						$.messager.alert('提示','订货数量填写不正确!数量不能大于到货数量!');
						return ;
					}
				},
				**/
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'inNum'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				toolbar:[
					{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
					}
					},'-',{
					id:'delete',
					text:'删除',
					disabled:false,
					iconCls:'icon-remove',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
						var rows = $('#table').datagrid('getSelections');
						if (rows.length > 0){
							for(var i=0;i<rows.length;i++){
								var row = rows[i];
								var index = $('#table').datagrid('getRowIndex', row);
								$('#table').datagrid('deleteRow', index);
							}
						}
						var data = $('#table').datagrid('getData');
						$('#table').datagrid('loadData',data);
					}
				}
			   ]
			});
			
		});
		
		function addDB(obj){
			//自动提交数据
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			//自动提交数据
			if($('#ff').form('validate')){
					var products="";
					var jsondata=$("#table").datagrid("getData");
					for(var i=0;i<jsondata.total;i++){
						if(products!=""){
							products+=";";
						}
						products+=jsonToStr(jsondata.rows[i]);
						var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
						if(jsondata.rows[i].inNum == null || jsondata.rows[i].inNum <= 0){
							$.messager.alert('提示','第'+index+'行数量填写不正确！');
							return ;
						}
						if((parseFloat(jsondata.rows[i].inNum)-(parseFloat(jsondata.rows[i].num)-parseFloat(jsondata.rows[i].receive_num)))>0){
							$.messager.alert('提示','第'+index+'行到货数量填写不正确!不能大于订货数量!');
							return ;
						}
					}
					if(!checkNullOREmpty(products,'产品为必填项')){return;}
					var code=jQuery("#code").val();
					var in_code=jQuery("#in_code").val();
					
					var param = 'code='+code+'&in_code='+in_code+'&purchase_id=<%=purchase_id%>&operation_date='+$('#operation_date').datetimebox('getValue')+'&remark='+$('#remark').val()+'&products='+products;
					$(obj).hide();
					$.ajax({
						type:'POST',
						<%=isAdd?"url:'"+contextPath+"/springMVC/warehouse/addPurchaseProductIn',":"url:'"+contextPath+"/springMVC/warehouse/updatePurchaseProductIn?product_in_id="+product_in_id+"',"%>
						data:param,
						dataType:'json',
						success:function(rtJson){
							if(rtJson.rtState == '0'){
								eval('window.opener.<%=callback%>();');
								//openDialogResize('<%=contextPath %>/springViews/warehouse/gdPurchaseProductIn.jsp?purchase_id=<%=purchase_id%>',1000, 800);	
								window.close();
							}
							else{
								$.messager.alert("提示",rtJson.rtMsrg);
							}
							$(obj).show();
						}
					});
			}
		}
		
		function gdPrint(){
			openDialogResize("<%=contextPath%>/springViews/print/startPurchaseProductInPrint.jsp?product_in_id=<%=product_in_id%>",1000, 800);
		}
	</script>
	</head>
	<body>
		<div style="width: 90%;padding-left:50px">
		<form id="ff">
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>订单编号：</td>
					<td class="td2" nowrap  >
						<input  id="code" name="code" readOnly>
					</td>
					<td align="right" class="td1" nowrap>入库单号：</td>
					<td class="td2" nowrap>
						<input  id="in_code" name="in_code" class="easyui-validatebox" data-options="required:true" >
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>工单编号：</td>
					<td class="td2" nowrap >
						<input  id="gd_code" name="gd_code" readOnly>
					</td>
					<td align="right" class="td1" nowrap>工单名称：</td>
					<td class="td2" nowrap >
						<input  id="gd_name" name="gd_name" readOnly>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>供货商：</td>
					<td class="td2" nowrap >
						<input  id="supplier" name="supplier" size="30" readOnly>
					</td>
					<td align="right" class="td1" nowrap>日期：</td>
					<td class="td2" nowrap >
						<input  id="operation_date" name="operation_date" class="easyui-datetimebox" data-options="required:true">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='3'>
						<textarea  id="remark" name="remark" cols="100" ></textarea>
					</td>
				</tr>
			 <%if(!"1".equals(isShow)){ %>
				<tr>
					<td colspan="4"  nowrap>
						<div align="center">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addDB(this);">提交</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
						</div>
					</td>
				</tr>
			<%} else{%>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='4'>
						<div align="left">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-print',plain:false" onclick="gdPrint();">打印预览</a>
						</div>
					</td>
				</tr>
			<%}%>
				<tr>
					<td colspan="4"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
			</table>
			</form>
		</div>
	</body>
</html>